//
//  CellofTrips.m
//  project-A (pianke)
//
//  Created by dllo on 16/2/1.
//  Copyright © 2016年 scott. All rights reserved.
//

#import "CellofTrips.h"
#import "CellTripTab.h"
#import "ModelOfTravl.h"

#import <UIImageView+WebCache.h>
#import "NetworkingHandle.h"
#import <MJRefresh.h>
#import <AFNetworking.h>
#import <DKNightVersion.h>
#define  WIDTH self.frame.size.width

#define HEIGHT self.frame.size.height

@interface CellofTrips ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,retain)NSMutableArray *arr;

@property (nonatomic,assign)NSInteger number;


@end

@implementation CellofTrips

-(void)dealloc{
    
    [_arr release];
    [_tabview release];
    [super dealloc];
    
}

#pragma 数据修改
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.number = 2;
        
        self.tabview = [[UITableView alloc]init];
        [self.contentView addSubview:self.tabview];
        self.tabview.backgroundColor = [UIColor brownColor];
        self.tabview.delegate = self;
        self.tabview.dataSource = self;
        
        [self.tabview registerClass:[CellTripTab class] forCellReuseIdentifier:@"pool1"];
        
        self.tabview.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor colorWithRed:0.16 green:0.14 blue:0.13 alpha:1]);
        
        
        self.tabview.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            
            NSLog(@"上拉刷新");
    
           
                

                
                
                [self.tabview.mj_footer endRefreshing];
                [self.tabview reloadData];

            
            
        }];
        
        [self.tabview release];
    }
    
    return self;
}

#pragma 取得数据
-(void)geturlwith:(NSString *)number{
    
    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    
    
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
    
    NSURL *URL = [NSURL URLWithString:number];
    
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];
    NSURLSessionDataTask *dataTask = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
        if (error) {
            NSLog(@"阿萨大大的");
        } else {
            self.arr = [NSMutableArray array];
          
            for (NSDictionary *dic in [responseObject objectForKey:@"trips"]) {
                                
                ModelOfTravl *model = [[ModelOfTravl alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                
                model.idd = [NSString stringWithFormat:@"%@",[dic objectForKey:@"id"]];
                
                [self.arr addObject:model];
                [model release];
            }
            [self.tabview reloadData];
        }
    }];
    [dataTask resume];
    

    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return WIDTH * 0.55 + 5;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ModelOfTravl *model = [self.arr objectAtIndex:indexPath.row];
    CellTripTab *cell = [tableView dequeueReusableCellWithIdentifier:@"pool1"];
    cell.name.text = model.name;
    
    NSString *str = [NSString stringWithFormat:@"%ld",model.days];
    NSString *str2 = [NSString stringWithFormat:@"%ld",model.photos_count];
    cell.date.text = [NSString stringWithFormat:@"%@ / %@天,%@图",model.start_date,str,str2];
    
    [cell.image sd_setImageWithURL:[NSURL URLWithString:model.front_cover_photo_url] placeholderImage:nil options:SDWebImageAvoidAutoSetImage];
    
    
    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.arr.count;
    
}


-(void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes{
    
    self.tabview.frame = self.contentView.frame;
    
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSLog(@"个人%@",indexPath);
    
    [self.delegate didClick:[self.arr objectAtIndex:indexPath.row]];
    
}


-(UICollectionViewLayoutAttributes *)preferredLayoutAttributesFittingAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes{
    
    return layoutAttributes;
    
}

-(void)layoutSubviews{
    
    [super layoutSubviews];
}

@end
